<?php


namespace app\test\controller;


use app\common\model\User;
use think\Controller;
use think\Db;

class ResetUen extends Controller
{
    /**
     * http://127.0.0.1/test/resetuen/getRepeatUenNum
     * 获取重复且不为空准了号数量
     * @return int|string
     * @throws \think\Exception
     */
    public function getRepeatUenNum()
    {
        $data = Db::table('wechat_backup_07_18_14_43_00.app_user')
            ->where('uen', '<>', '')
            ->whereIn('uen', function ($query)
            {
                $query->table('wechat_backup_07_18_14_43_00.app_user')->group('uen')->having('count(uen)>1')->field('uen');
            })->count();

        return $data;
    }

    /**
     * http://127.0.0.1/test/resetuen/getEmptyUenNum
     * 获取不为空准了号数量
     * @return int|string
     * @throws \think\Exception
     */
    public function getEmptyUenNum()
    {
        $data = Db::table('wechat_backup_07_18_14_43_00.app_user')
            ->where('uen', '')
            ->whereIn('uen', function ($query)
            {
                $query->table('wechat_backup_07_18_14_43_00.app_user')->group('uen')->having('count(uen)>1')->field('uen');
            })->count();

        return $data;
    }


    public function resetRepeatUen()
    {
        $data = Db::table('wechat_backup_07_18_14_43_00.app_user')
            ->where('uen', '<>', '')
            // ->where('id', '<', 10035290)
            ->whereIn('uen', function ($query)
            {
                $query->table('wechat_backup_07_18_14_43_00.app_user')->group('uen')->having('count(uen)>1')->field('uen');
            })->field('id,uen')->limit(100)->select();

        $num = Db::table('wechat_backup_07_18_14_43_00.app_user')
            ->where('uen', '<>', '')
            // ->where('id', '<', 10034790)
            ->whereIn('uen', function ($query)
            {
                $query->table('wechat_backup_07_18_14_43_00.app_user')->group('uen')->having('count(uen)>1')->field('uen');
            })->count();

        echo '<pre>';
        echo $num.PHP_EOL;
        print_r( $data);



        die;

        $time = time();
        $limit = 100;

        $data = Db::table('wechat_backup_07_18_14_43_00.app_user')
            // ->where('uen', '<>', '')
            ->whereIn('uen', function ($query)
            {
                $query->table('wechat_backup_07_18_14_43_00.app_user')->group('uen')->having('count(uen)>1')->field('uen');
            })->field('id,uen')->paginate($limit)->toArray();

        $uenArray = array_unique($this->getUen($limit));

        $updateData = [];
        foreach ($data['data'] as $key => $value)
        {
            $uen = $uenArray[$key] ?? current($this->getUen(1));
            Db::table('wechat_backup_07_18_14_43_00.app_user')->where('id', $value['id'])->update(['uen' => $uen]);
            // array_push($updateData, [
            //     'id' => $value['id'],
            //     'uen' =>
            // ]);
        }

        var_export(time() - $time);
        // (new User())->saveAll($updateData);

    }

    public function getUen($num)
    {
        $uenArr = [];
        for ($i = 0; $i < $num; $i++)
        {
            $uen = substr(md5(microtime() . $i . mt_rand(0, 1000000) . uniqid()), 0, 12);
            array_push($uenArr, $uen);
        }

        return $uenArr;
    }
}

